package main;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import dbTable.RETOPIC;
import dbTable.RETOP_SUB;
import dbTable.TOPIC;
import dbTable.ACCOUNT;
import dbTable.ACC_SUB;
import dbTable.TOP_SUB;

public class MainBean {
	//個人資料
	private String accountid;
	private String name;
	private String email;
	private String nickName;
	//文章
	private String all;
	private String topicName;
	private String content;
	private int[] reTopicCount;
	private String reTopicTime;
	private String reTopicName;
	private long howLong;
	
	private String exe;
	private ArrayList<String> err = new ArrayList<String>();
	public static final int OK = 1;
	public static final int FAIL = 2;
	
	
	public String getExe() {
		ACCOUNT a = new ACCOUNT();		
		ACC_SUB b = new ACC_SUB();
		TOP_SUB tt = new TOP_SUB();
		RETOP_SUB rett = new RETOP_SUB();
		
		//取出該作者的ID
		String[][] account = b.select_accountid(email);
		accountid = account[0][0];
		
		System.out.println("帳號id="+accountid);
		//取出該帳號有幾篇文章
		String[][] topic = tt.select_reply(accountid);
		String[][] retopic = new String[topic.length][topic[0].length];
		System.out.println("文章ID="+topic[0][0]);
		
		//取出作者ID
		String[] topicid = new String[topic.length];
		//取出回文者的時間
		String[] retopicTime = new String[topic.length];
		String[] time = new String[topic.length];
		reTopicCount = new int[topic.length];
		for(int i = 0 ;i <topic.length ;i++){
			
			topicid[i] = topic[i][0];

			if(rett.select_replyid(topicid[i])==null){
				reTopicCount[i]=0;
				retopic[i][0]="無回應";
				retopicTime[i] = "無回應";
				time[i] = "無回應";
			}else{
				System.out.println("回文數字1 = "+rett.select_count(topicid[i]).length);
				//取出每篇最後的回文者ID
				reTopicCount[i]=rett.select_count(topicid[i]).length;
				retopic[i] = rett.select_replyid(topicid[i])[0];
				long myTime =  Long.parseLong(retopic[i][4]);
				SimpleDateFormat f = new SimpleDateFormat("a hh:mm yyyy/MM/dd");
				SimpleDateFormat f2 = new SimpleDateFormat("dd天:hh時:mm分 ");
				howLong = System.currentTimeMillis() - myTime;		
				retopicTime[i] = f.format(myTime);
				time[i] = f2.format(howLong);
				
			}
			System.out.println("回文數字2 = "+reTopicCount[i]);
		}
		//取出每篇主題最後的回文者姓名
		String[][] retopicName = new String[topic.length][topic[0].length];

		for(int i = 0 ;i <topic.length ;i++){
			if(retopic[i][0]!="無回應"){
				retopicName[i] = a.select(Integer.parseInt(retopic[i][2].toString()))[0];
				
			}else{
				retopicName[i][0] = "";
				retopicName[i][1] = "無回應";
			}
			System.out.println("回覆者:"+retopicName[i][1]);
		}
		
		all = new String();

		for(int i = 0 ; i < topic.length ; i++){
			topicName = topic[i][1];
			content = topic[i][4];
			name = account[0][1];
			reTopicTime = retopicTime[i];
			reTopicName = retopicName[i][1];

			
			all += "<tr><td>"+topicName+"</td>";
			all += "<td>"+content+"</td>";
			all += "<td>"+reTopicCount[i]+"</td>";
			all += "<td>"+name+"</td>";
			all += "<td>"+reTopicTime+"</td>";
			all += "<td>"+reTopicName+"</td>";
			all += "<td>"+time[i]+"</td></tr>";
		}
	
		return exe;
	}
	
	public String getAll() {
		return all;
	}

	public void setAll(String all) {
		this.all = all;
	}

	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getNickName() {
		return nickName;
	}
	public void setNickName(String nickName) {
		this.nickName = nickName;
	}
	public String getTopicName() {
		return topicName;
	}
	public void setTopicName(String topic) {
		this.topicName = topic;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}

	public int[] getReTopicCount() {
		return reTopicCount;
	}

	public void setReTopicCount(int[] reTopicCount) {
		this.reTopicCount = reTopicCount;
	}

	public String getReTopicTime() {
		return reTopicTime;
	}
	public void setReTopicTime(String reTopicTime) {
		this.reTopicTime = reTopicTime;
	}
	public String getReTopicName() {
		return reTopicName;
	}
	public void setReTopicName(String reTopicName) {
		this.reTopicName = reTopicName;
	}
	public long getHowLong() {
		return howLong;
	}
	public void setHowLong(long howLong) {
		this.howLong = howLong;
	}
	public void setExe(String exe) {
		this.exe = exe;
	}
	public ArrayList<String> getErr() {
		return err;
	}
	public void setErr(ArrayList<String> err) {
		this.err = err;
	}
	
}
